package com.daigoudao.web.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.config.ConfigManager;
import com.daigoudao.util.Constants;
import com.daigoudao.util.FileUtil;
import com.daigoudao.util.ImageUtil;
import com.daigoudao.util.StringUtils;
import com.daigoudao.web.common.BasePageAction;
import com.daigoudao.web.model.AccCheckModel;
import com.daigoudao.web.model.AccountModel;
import com.daigoudao.web.model.AreaModel;
import com.daigoudao.web.model.Cart;
import com.daigoudao.web.model.CategoryModel;
import com.daigoudao.web.model.FollowModel;
import com.daigoudao.web.model.ShortMessage;
import com.daigoudao.web.model.UserAddrModel;
import com.daigoudao.web.model.UserAreaModel;
import com.daigoudao.web.model.UserCategoryModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.service.IAccountService;
import com.daigoudao.web.service.ICommonService;
import com.daigoudao.web.service.IShortMessageService;
import com.daigoudao.web.service.IUserAddrService;
import com.daigoudao.web.service.IUserAttrService;
import com.daigoudao.web.service.IUserService;
import com.daigoudao.web.service.MailSendService;
import com.danga.MemCached.MemCachedClient;

public class UserAction extends BasePageAction {

	private static final long serialVersionUID = -5150006703156379466L;

	private static Logger logger = LoggerFactory.getLogger(UserAction.class);

	private IUserService userService;

	// 获取区域列表
	private ICommonService commonService;
	private MailSendService mailSendService;

	private IAccountService accountService;

	private IUserAttrService userAttrService;

	public IUserAttrService getUserAttrService() {
		return userAttrService;
	}

	public void setUserAttrService(IUserAttrService userAttrService) {
		this.userAttrService = userAttrService;
	}

	private IUserAddrService userAddrService;

	private MemCachedClient memcachedClient;

	private IShortMessageService shortMessageService;

	private List<UserAddrModel> userAddrList;

	// 增加地址
	private UserAddrModel userAddrModel;

	// 账户信息
	private AccountModel accountModel;

	private UserModel userModel;

	// 用户列表
	private List<UserModel> userList;

	private List<AreaModel> serviceAreaList;
	private List<CategoryModel> serviceTypeList;
	private List<AccCheckModel> accCheckList;

	private File cardFile;
	private String cardFileFileName;
	private String cardFileContentType;
	private String cardType;
	private String cardNum;
	private String realName;
	private String t;
	private String email;
	private String v;
	private String cssClass = "tips7";

	public List<UserModel> getUserList() {
		return userList;
	}

	public void setUserList(List<UserModel> userList) {
		this.userList = userList;
	}

	public UserAddrModel getUserAddrModel() {
		return userAddrModel;
	}

	public void setUserAddrModel(UserAddrModel userAddrModel) {
		this.userAddrModel = userAddrModel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getV() {
		return v;
	}

	public void setV(String v) {
		this.v = v;
	}

	public String getT() {
		return t;
	}

	public void setT(String t) {
		this.t = t;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	// 服务端返回的提示信息
	private String tipMessage;

	public String getTipMessage() {
		return tipMessage;
	}

	public void setMemcachedClient(MemCachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}

	public void setTipMessage(String tipMessage) {
		this.tipMessage = tipMessage;
	}

	public IAccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}

	public IUserAddrService getUserAddrService() {
		return userAddrService;
	}

	public void setUserAddrService(IUserAddrService userAddrService) {
		this.userAddrService = userAddrService;
	}

	public void setShortMessageService(IShortMessageService shortMessageService) {
		this.shortMessageService = shortMessageService;
	}

	public List<UserAddrModel> getUserAddrList() {
		return userAddrList;
	}

	public void setUserAddrList(List<UserAddrModel> userAddrList) {
		this.userAddrList = userAddrList;
	}

	public AccountModel getAccountModel() {
		return accountModel;
	}

	public void setAccountModel(AccountModel accountModel) {
		this.accountModel = accountModel;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public String getCardFileContentType() {
		return cardFileContentType;
	}

	public void setCardFileContentType(String cardFileContentType) {
		this.cardFileContentType = cardFileContentType;
	}

	public String getCardType() {
		return cardType;
	}

	public void setCardType(String cardType) {
		this.cardType = cardType;
	}

	public String getCardNum() {
		return cardNum;
	}

	public void setCardNum(String cardNum) {
		this.cardNum = cardNum;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getCardFileFileName() {
		return cardFileFileName;
	}

	public void setCardFileFileName(String cardFileFileName) {
		this.cardFileFileName = cardFileFileName;
	}

	public File getCardFile() {
		return cardFile;
	}

	public void setCardFile(File cardFile) {
		this.cardFile = cardFile;
	}

	public List<AccCheckModel> getAccCheckList() {
		return accCheckList;
	}

	public void setAccCheckList(List<AccCheckModel> accCheckList) {
		this.accCheckList = accCheckList;
	}

	public UserModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public List<AreaModel> getServiceAreaList() {
		return serviceAreaList;
	}

	public void setServiceAreaList(List<AreaModel> serviceAreaList) {
		this.serviceAreaList = serviceAreaList;
	}

	public List<CategoryModel> getServiceTypeList() {
		return serviceTypeList;
	}

	public void setServiceTypeList(List<CategoryModel> serviceTypeList) {
		this.serviceTypeList = serviceTypeList;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public MailSendService getMailSendService() {
		return mailSendService;
	}

	public void setMailSendService(MailSendService mailSendService) {
		this.mailSendService = mailSendService;
	}

	public String initRegister() {
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String register() {
		UserModel result = null;
		try {
			result = userService.saveUser(userModel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("register error:", e);
			}
			return ERROR;
		}

		if (result != null) {
			// 注册成功就转为登录状态
			session.put(Constants.USER_INFO, result);

			// 注册成功发送欢迎邮件
			String to = userModel.getEmail();
			try {
				mailSendService.sendHtmlEmail(to);
			} catch (Exception e) {
				if (logger.isErrorEnabled()) {
					logger.error("register error:", e);
				}
			}
		} else {
			return ERROR;
		}

		return SUCCESS;
	}

	public void isSameEmail() {
		String email = request.getParameter("email");

		try {
			boolean result = userService.getSameEmail(email);
			response.getWriter().print(result);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("isSameEmail error:", e);
			}
		}
	}

	public void isSameCaptcha() {
		String rcaptcha = request.getParameter("captcha").toLowerCase();
		String captcha = session.get(Constants.CAPTCHA_ID).toString()
				.toLowerCase();

		try {
			response.getWriter().print(captcha.equals(rcaptcha));
		} catch (IOException e) {
			if (logger.isErrorEnabled()) {
				logger.error("isSameCaptcha error:", e);
			}
		}
	}

	public String userValid() {
		// String valid = request.getParameter("v");
		// String email = request.getParameter("email");
		return SUCCESS;
	}

	public String myhome() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		if (userModelSession.getUserType() == 2) {
			return "agent";
		} else {
			return "user";
		}
	}

	public String initRetrievePassword() {
		return SUCCESS;
	}

	public String retrievePassword() {

		email = userModel.getEmail();
		v = StringUtils.getUserValid(email);
		userModel.setValidCode(v);
		userModel.setSendTime(System.currentTimeMillis());

		try {
			userService.updateUserByEmail(userModel);
			mailSendService.sendHtmlEmail(email, v);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("retrievePassword error:", e);
			}
			return ERROR;
		}

		return SUCCESS;
	}

	public String initUpdatePassword() {

		String rootUrl = ConfigManager.getConfigString("common.rootUrl");

		if (email == null || "".equals(email) || v == null || "".equals(v)) {
			backUrl = rootUrl;
			responseMsg = "出错啦！您访问的链接不存在! 5秒钟后返回<a style='color:#A10000' href='"
					+ backUrl + "'>首页</a>。";
			return ERROR;
		}

		userModel = new UserModel();
		userModel.setEmail(email);
		userModel.setValidCode(v);

		try {
			int result = userService.getCheckUserPwd(userModel);
			if (0 == result) {
				backUrl = rootUrl;
				responseMsg = "出错啦！您访问的链接不存在! 5秒钟后返回<a style='color:#A10000' href='"
						+ backUrl + "'>首页</a>。";
				return ERROR;
			} else if (1 == result) {
				backUrl = rootUrl + "/retrievepassword.htm";
				responseMsg = "对不起！您访问的链接已过期! 5秒钟后返回<a style='color:#A10000' href='"
						+ backUrl + "'>找回密码页</a>。";
				return ERROR;
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("initUpdatePassword error:", e);
			}
			return ERROR;
		}

		return SUCCESS;
	}

	public String updatePassword() {

		try {
			boolean flag = userService.updateUserPwd(userModel);
			if (flag) {
				cssClass = "tips6";
				backUrl = ConfigManager.getConfigString("common.rootUrl");
				responseMsg = "恭喜你，密码修改成功！5秒钟后返回<a style='color:#A10000' href='"
						+ backUrl + "'>首页</a>。";
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updatePassword error:", e);
			}
			return ERROR;
		}

		return SUCCESS;
	}

	public String initLogin() {
		return SUCCESS;
	}

	public String login() {
		/*
		 * String ip = request.getRemoteAddr(); UserModel result = null; try {
		 * result = userService.getUserLogin(userModel); } catch (Exception e) {
		 * e.printStackTrace(); }
		 * 
		 * if (result != null) { String lastTime = StringUtils.getCurrentTime();
		 * userModel.setUserid(result.getUserid()); userModel.setLastIp(ip);
		 * userModel.setLastTime(lastTime); try {
		 * userService.updateUserLogin(userModel); } catch (Exception e) {
		 * e.printStackTrace(); }
		 * 
		 * session.put(Constants.USER_INFO, result); } else {
		 * 
		 * return ERROR; }
		 */

		backUrl = (String) session.get(Constants.URL);
		if (backUrl == null || "".equals(backUrl)) {
			backUrl = "accountinit.htm";
		}

		session.remove(Constants.URL);

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public void aLogin() {
		String data = "";

		String ip = request.getRemoteAddr();
		String email = request.getParameter("email");
		String password = request.getParameter("pwd");

		userModel = new UserModel();
		userModel.setEmail(email);
		userModel.setPassword(password);

		UserModel result = null;
		try {
			result = userService.getUserLogin(userModel);

			if (result != null) {
				String lastTime = StringUtils.getCurrentTime();
				int userId = result.getUserid();
				userModel.setUserid(userId);
				userModel.setLastIp(ip);
				userModel.setLastTime(lastTime);
				userService.updateUserLogin(userModel);

				data = userId + "|" + result.getNickName() + "|"
						+ result.getUserType();
				String url = (String) session.get(Constants.URL);
				if (url != null) {
					data += "|" + url;
				}
				session.put(Constants.USER_INFO, result);
				Cart cart = getCart(memcachedClient, userId);
				float rate = userAttrService.getUserAttr(userId).getRate();
				// 将代购车中的商品数量放入至session中
				session.put(Constants.CART, cart.getCount());
				session.put(Constants.RATE, rate);
			}

			response.getWriter().print(data);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("aLogin error:", e);
			}
		}
	}

	public String logout() {
		session.clear();
		return SUCCESS;
	}

	public String modifyPassInit() {
		if (!isLogin()) {
			return LOGIN;
		}
		return SUCCESS;
	}

	public String accountManageInit() {
		if (!isLogin()) {
			return LOGIN;
		}
		try {
			serviceTypeList = commonService.queryCategorys();
			serviceAreaList = commonService.queryAreas();

		} catch (Exception e) {
			logger.error("accountManageInit error", e);
			return ERROR;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		try {
			userModel = userService.getUserBaseInfoByID(userid);

			// area
			List<UserAreaModel> temp = userService.getUserAreaList(userid);
			if (temp != null) {
				int[] areaids = new int[temp.size()];
				for (int i = 0; i < temp.size(); i++) {
					areaids[i] = temp.get(i).getAreaid();
				}
				userModel.setArrArea(areaids);
			}

			// category
			List<UserCategoryModel> temp2 = userService
					.getUserCategoryList(userid);
			if (temp2 != null) {
				int[] categoryids = new int[temp2.size()];
				for (int i = 0; i < temp2.size(); i++) {
					categoryids[i] = temp2.get(i).getCategoryid();
				}
				userModel.setArrCat(categoryids);
			}

		} catch (Exception e) {
			logger.error("getUserBaseInfoByID", e);
		}

		if ("2".equals(t)) {
			return "BEAGENT";
		}
		return SUCCESS;
	}

	public String setBaseInfo() {
		// 从userModel取数据保存
		accountManageInit();
		return SUCCESS;
	}

	/**
	 * 用户修改密码
	 * 
	 * @return
	 */
	public String modifyPass() {
		if (!isLogin()) {
			return LOGIN;
		}

		UserModel userModelSession = getUserLogin();
		// 从userModel取数据修改密码
		int result = userService.updateUserPasswd(userModelSession.getUserid(),
				userModel.getPassword(), userModel.getNewpassword());
		switch (result) {
		case 0: {
			tipMessage = "密码修改成功!";
			break;
		}
		case -1: {
			tipMessage = "当前密码输入错误!";
			break;
		}
		case 1: {
			tipMessage = "密码修改失败,请稍后再试!";
			break;
		}
		}
		return SUCCESS;
	}

	public String sendEmail() {

		return SUCCESS;
	}

	public String emailCheck() {
		return SUCCESS;
	}

	/**
	 * 公用的初始化方法
	 * 
	 * @return
	 */
	public String init() {
		if (!isLogin()) {
			return LOGIN;
		}
		return SUCCESS;
	}

	/**
	 * 资金账户设置
	 * 
	 * @return
	 */
	public String accountInit() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		try {
			accountModel = accountService.getAccountInfo(userModelSession
					.getUserid());
		} catch (Exception e) {
			logger.error("getAccountInfo", e);
		}
		// 默认设置为支付宝
		if ("2".equals(t)) {
			return "BEAGENT";
		}

		return SUCCESS;
	}

	public String saveAccount() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		accountModel.setUserid(userModelSession.getUserid());
		accountService.saveAccountInfo(accountModel);
		accountModel = accountService.getAccountInfo(userid);
		setTipMessage("账户信息保存成功!");
		return SUCCESS;
	}

	public String userAddrInit() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		try {
			userAddrList = userAddrService.getAddrListByUserID(userModelSession
					.getUserid());
		} catch (Exception e) {
			logger.error("getAddrListByUserID error!", e);
		}
		return SUCCESS;
	}

	public String getUserAddrInfo() {
		if (!isLogin()) {
			return LOGIN;
		}
		userAddrModel = userAddrService.getAddrByAddrID(userAddrModel
				.getAddrId());
		userAddrInit();
		return SUCCESS;
	}

	public String updateUserAddr() {
		if (!isLogin()) {
			return LOGIN;
		}

		userAddrModel.setAddress(userAddrModel.getProvince()
				+ userAddrModel.getCity() + userAddrModel.getStreet());
		try {
			userAddrService.updateUserAddr(userAddrModel);
		} catch (Exception e) {
			logger.error("update User address!!", e);
		}
		userAddrModel = null;
		userAddrInit();
		return SUCCESS;
	}

	public String saveUserAddr() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		// 设置userid
		userAddrModel.setUserId(userid);
		// 合并地址
		userAddrModel.setAddress(userAddrModel.getProvince()
				+ userAddrModel.getCity() + userAddrModel.getStreet());
		try {
			userAddrService.saveAddr(userAddrModel);
		} catch (Exception e) {
			logger.error("saveAddr", e);
		}
		userAddrModel = null;
		userAddrList = userAddrService.getAddrListByUserID(userid);
		return SUCCESS;
	}

	/**
	 * 设置默认地址
	 * 
	 * @return
	 */
	public String setDefaultAddr() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		try {
			userAddrService.setDefaultAddr(userAddrModel);
		} catch (Exception e) {
			logger.error("setDefaultAddr error", e);
		}
		userAddrList = userAddrService.getAddrListByUserID(userid);
		return SUCCESS;
	}

	public String deleteAddr() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		try {
			userAddrService.deleteAddr(userAddrModel);
		} catch (Exception e) {
			logger.error("deleteAddr error!", e);
		}
		userAddrList = userAddrService.getAddrListByUserID(userid);
		return SUCCESS;
	}

	public String saveBaseInfo() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		userModel.setUserid(userid);
		try {
			userService.updateUserInfo(userModel);
		} catch (Exception e) {
			logger.error("saveUser error" + userModel.toString(), e);
		}
		// Save area

		userService.updateUserAreaList(userid, userModel.getArrArea());
		userService.updateUserCategoryList(userid, userModel.getArrCat());

		userModel = userService.getUserBaseInfoByID(userid);
		try {
			serviceTypeList = commonService.queryCategorys();
			serviceAreaList = commonService.queryAreas();
		} catch (Exception e) {
			logger.error("saveUser error" + userModel.toString(), e);
			return ERROR;
		}

		List<UserAreaModel> temp = userService.getUserAreaList(userid);
		if (temp != null) {
			int[] areaids = new int[temp.size()];
			for (int i = 0; i < temp.size(); i++) {
				areaids[i] = temp.get(i).getAreaid();
			}
			userModel.setArrArea(areaids);
		}

		List<UserCategoryModel> temp2 = userService.getUserCategoryList(userid);
		if (temp2 != null) {
			int[] categoryids = new int[temp2.size()];
			for (int i = 0; i < temp2.size(); i++) {
				categoryids[i] = temp2.get(i).getCategoryid();
			}
			userModel.setArrCat(categoryids);
		}

		setTipMessage("信息保存成功!");
		return SUCCESS;
	}

	public String saveImportInfo() {
		if (!isLogin()) {
			return LOGIN;
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		userModel.setUserid(userid);
		try {
			userService.updateUserInfo(userModel);
		} catch (Exception e) {
			logger.error("saveUser error" + userModel.toString(), e);
		}
		// Save area

		userService.updateUserAreaList(userid, userModel.getArrArea());
		userService.updateUserCategoryList(userid, userModel.getArrCat());

		userModel = userService.getUserBaseInfoByID(userid);
		try {
			serviceTypeList = commonService.queryCategorys();
			serviceAreaList = commonService.queryAreas();
		} catch (Exception e) {
			logger.error("saveUser error" + userModel.toString(), e);
			return ERROR;
		}

		List<UserAreaModel> temp = userService.getUserAreaList(userid);
		if (temp != null) {
			int[] areaids = new int[temp.size()];
			for (int i = 0; i < temp.size(); i++) {
				areaids[i] = temp.get(i).getAreaid();
			}
			userModel.setArrArea(areaids);
		}

		List<UserCategoryModel> temp2 = userService.getUserCategoryList(userid);
		if (temp2 != null) {
			int[] categoryids = new int[temp2.size()];
			for (int i = 0; i < temp2.size(); i++) {
				categoryids[i] = temp2.get(i).getCategoryid();
			}
			userModel.setArrCat(categoryids);
		}

		setTipMessage("信息保存成功!");
		return SUCCESS;
	}

	public String getMyAttentionAgent() {
		if (!isLogin()) {
			return LOGIN;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Start getMyAttentionAgent");
		}
		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		userModel = new UserModel();
		userModel.setUserid(userid);

		// 查询列表
		try {
			userList = userService.getFollowList(userModel);
		} catch (Exception e) {
			logger.error("getFollowList userid:" + userModel.getUserid(), e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 取消关注
	 * 
	 * @return
	 */
	public String deleteFollowAgent() {
		if (!isLogin()) {
			return LOGIN;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Start getMyAttentionAgent");
		}
		try {
			userService.deleteFollow(id);
		} catch (Exception e) {
			logger.error("delete Follow Error id={}", id, e);
		}

		return SUCCESS;
	}

	public String getBrokerDetail() {
		try {
			FollowModel follow = new FollowModel();
			follow.setFollower(id);
			follow.setPageSize(getPageSize());
			follow.setStartIndex((getSuperPage() - 1) * getPageSize());

			UserModel user = super.getUserLogin();
			if (user != null) {
				int userId = user.getUserid();
				follow.setUserId(userId);
			}

			userModel = userService.getBrokerDetail(follow);
			setTotalSize(userModel.getCanBuyCount());
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBrokerDetail error:", e);
			}
			return ERROR;
		}

		return SUCCESS;
	}

	public void sendMsg() {
		int receiver = Integer.parseInt(request.getParameter("userId"));
		String content = request.getParameter("content");
		int sender = super.getUserLogin().getUserid();

		ShortMessage msg = new ShortMessage();
		msg.setReceiver(receiver);
		msg.setSender(sender);
		msg.setContent(content);
		msg.setCreatetime(new Date());
		msg.setType("0");

		try {
			boolean flag = shortMessageService.saveShortMessage(msg);
			response.getWriter().print(flag);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("sendMsg error:", e);
			}
		}
	}

	// 头像上传
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	private int photoTag = 0;

	public int getPhotoTag() {
		return photoTag;
	}

	public void setPhotoTag(int photoTag) {
		this.photoTag = photoTag;
	}

	// 头像上传
	public String photoUpload() {
		if (!isLogin()) {
			return LOGIN;
		}

		String root = ConfigManager.getConfigString("upload.resDir");
		String path = StringUtils.formatDate(new Date(), "yyyy-MM-dd");
		String basePath = root + File.separator + path;
		FileUtil.createDir(basePath);

		UserModel userModelSession = getUserLogin();
		int userid = userModelSession.getUserid();
		if (userModel == null) {
			userModel = new UserModel();
		}
		userModel.setUserid(userid);

		// 循环上传的文件
		for (int i = 0; i < file.size(); i++) {
			// 获取文件扩展名
			String fileName = this.getFileFileName().get(i);
			String extName = FileUtil.getExtention(fileName);

			fileName = StringUtils.getUUID() + extName;
			File destFile = new File(basePath, fileName);

			// 文件复制
			try {
				FileUtil.copy(file.get(i), destFile);
			} catch (Exception e) {
			}

			// 处理文件
			try {
				boolean result = ImageUtil.resize120(destFile.getPath());
				if (!result) {
					tipMessage = "上传失败,你上传的格式不符合要求,请重新上传!";
					break;
				}
				ImageUtil.resize50(destFile.getPath());
			} catch (IOException e) {
				logger.error("ImageUtil resize120 ERROR", e);
				tipMessage = "系统忙,请稍后再试!";
				return SUCCESS;
			}

			// 文件在数据库存储
			String urlPath = File.separator + path + File.separator + fileName;
			// 保存到数据库
			userModel.setPhoto(urlPath);
			userService.updateUserInfo(userModel);
			// 更新session
			getUserLogin().setPhoto(urlPath);
		}
		accountManageInit();
		photoTag = 1;
		if ("2".equals(t)) {
			return "BEAGENT";
		}
		return SUCCESS;
	}

}
